$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'factory/queryList',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', width: 80,hidden:true },
            { label: '名称', name: 'factoryname', width: 60 },
            { label: '地址', name: 'adress', width: 80},
            { label: '域名', name: 'factoryURL', width: 100 },
            { label: '联系人', name: 'contacts', width: 40 },
            { label: '联系电话', name: 'phone', width: 60 },
            { label: '前端路由', name: 'router', width: 60 },
            {
                label: '操作', name: 'id', width: 100, formatter: function (value, options, row) {

                    var r = '<a class="little-btn edit-btn" onclick="updateS(\'' + value + '\')"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>' +
                        '&nbsp;&nbsp;<a class="little-btn del-btn" onclick="delS(\'' + value + '\')"><i class="fa fa-trash-o"></i>&nbsp;删除</a>'

                    return r;
                    // '             <a class="btn btn-primary"><i class="fa fa-trash-o"></i>&nbsp;删除</a>';
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
function updateS(id) {
    $.get(baseURL + "factory/info/"+id, function(r){
        vm.showList = false;
        vm.title = "修改";
        vm.factoryInfo = r.data;
    });

}

function delS(id) {
    confirm('确定要删除选中的记录？', function(){
        $.ajax({
            type: "POST",
            url: baseURL + "factory/delete",
            contentType: "application/json",
            data: JSON.stringify(id),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}







var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        factoryInfo: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.factoryInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "ClassCourse2/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.ClassCourse2 = r.ClassCourse2;
                vm.className = r.ClassCourse2.className;
            });
        },
        upload: function () {

        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "ClassCourse2/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.factoryInfo.id  ==  null ? "factory/insert" : "factory/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.factoryInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.factoryInfo.factoryname)){
                alert("工厂名称不能为空");
                return true;
            }
            if(isBlank(vm.factoryInfo.factoryURL)){
                alert("域名不能为空");
                return true;
            }
        }
    }
});